import axios from "axios"
import cookies from "./cookies"
import toast from "../components/Toast"
import dialog from "../components/Dialog"

const authException = async message => {
	await dialog.show("登录失效");
	console.log("resolve!!!!")

}

axios.interceptors.request.use(config => {
	config.loginRequired = config.loginRequired === undefined ? true : config.loginRequired;

	if(config.loginRequired){
		const token = cookies.get("token")
		if(!token){
			Promise.reject("auth failed")
			return authException()
		}
	}

	return config
}, err => Promise.reject(err))

export default class http {
	static get(url, params = {}, cfg = {}){
		return axios.get(envs.rootPath + url, {params, ...cfg})
	}
	static post(url, params = {}, cfg = {}){
		return axios.post(envs.rootPath + url, params, cfg)
	}

}
